package com.xuelang.pipeline.infrastructure.pipeline.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.pipeline.dto.DeploymentUnitQuery;
import com.xuelang.pipeline.domain.pipeline.DeploymentUnit;
import com.xuelang.pipeline.domain.pipeline.repository.DeploymentUnitRepository;
import com.xuelang.pipeline.infrastructure.pipeline.converter.DeploymentUnitToDOConverter;
import com.xuelang.pipeline.infrastructure.pipeline.dataobject.DeploymentUnitDO;
import com.xuelang.pipeline.infrastructure.pipeline.mapper.DeploymentUnitMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>部署单仓库实现类</p>
 *
 * @author : yixin.lys
 * @date : 2024-06-25 16:50
 **/
@Slf4j
@Repository
public class DeploymentUnitRepositoryImpl implements DeploymentUnitRepository {

    @Autowired
    private DeploymentUnitMapper mapper;

    @Override
    public PageRecord<DeploymentUnit> pageByCondition(long currentPage, long pageSize, DeploymentUnitQuery condition) {
        currentPage = currentPage < 1 ? 1 : currentPage;
        pageSize = pageSize < 1 ? 10 : pageSize;
        Page<DeploymentUnitDO> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<DeploymentUnitDO> wrapper = Wrappers.lambdaQuery(DeploymentUnitDO.class);

        String pipelineInstanceCode = condition.getPipelineInstanceCode();
        String componentLabel = condition.getComponentLabel();
        String environmentUuid = condition.getEnvironmentUuid();
        // 根据流水线实例编码过滤
        wrapper.eq(StringUtils.isNotBlank(pipelineInstanceCode), DeploymentUnitDO::getPipelineInstanceCode, pipelineInstanceCode);
        // 根据组件label过滤
        wrapper.eq(StringUtils.isNotBlank(componentLabel), DeploymentUnitDO::getComponentLabel, componentLabel);
        // 根据环境uuid过滤
        wrapper.eq(StringUtils.isNotBlank(environmentUuid), DeploymentUnitDO::getEnvironmentUuid, environmentUuid);
        wrapper.orderByDesc(DeploymentUnitDO::getCreateTime);
        Page<DeploymentUnitDO> pageResult = mapper.selectPage(page, wrapper);
        if (Objects.nonNull(pageResult)) {
            return new PageRecord<>(
                    page.getCurrent(),
                    page.getSize(),
                    page.getTotal(),
                    page.getPages(),
                    page.getRecords().stream().map(DeploymentUnitToDOConverter.INSTANCE::fromDO).collect(Collectors.toList())
            );
        }
        return null;
    }

    @Override
    public void add(DeploymentUnit deploymentUnit) {
        mapper.insert(DeploymentUnitToDOConverter.INSTANCE.toDO(deploymentUnit));
    }

    @Override
    public void update(DeploymentUnit deploymentUnit) {
        mapper.updateById(DeploymentUnitToDOConverter.INSTANCE.toDO(deploymentUnit));
    }

    @Override
    public DeploymentUnit queryByPipelineInstanceCode(String pipelineInstanceCode) {
        if (StringUtils.isBlank(pipelineInstanceCode)) {
            return null;
        }
        LambdaQueryWrapper<DeploymentUnitDO> wrapper = Wrappers.lambdaQuery(DeploymentUnitDO.class);
        wrapper.eq(DeploymentUnitDO::getPipelineInstanceCode, pipelineInstanceCode);
        return DeploymentUnitToDOConverter.INSTANCE.fromDO(mapper.selectOne(wrapper));
    }

    @Override
    public DeploymentUnit queryById(String id) {
        DeploymentUnitDO deploymentUnitDO = mapper.selectOne(Wrappers.lambdaQuery(DeploymentUnitDO.class).eq(DeploymentUnitDO::getId, id));
        return DeploymentUnitToDOConverter.INSTANCE.fromDO(deploymentUnitDO);
    }

    @Override
    public void removeByIds(List<String> idList) {
        mapper.deleteBatchIds(idList);
    }

    @Override
    public void removeByCodes(List<String> codeList) {
        LambdaQueryWrapper<DeploymentUnitDO> wrapper = Wrappers.lambdaQuery(DeploymentUnitDO.class);
        wrapper.in(DeploymentUnitDO::getPipelineInstanceCode, codeList);
        mapper.delete(wrapper);
    }
}
